/************************************************************************/
/*    author  vinee.han
/*    time    2015/06/08
/*    BNOUOJ                                                                    */
/************************************************************************/

#include <iostream>
#include <queue>
#include <fstream>
using namespace std;

int row,col;
int cnt = 0;
char arr[301][301];
bool visted[301][301];
int step[4][2] = {{-1,0},{0,1},{1,0},{0,-1}};

void fcino()
{
	freopen("in.txt","r",stdin);
	freopen("out.txt","w",stdout);
}

void fexit()
{
	fclose(stdin);
	fclose(stdout);
}

struct point
{
	int prerow,precol;
	int row,col;
};

point resque[10000];

void bfs(point temp)
{
	queue<point> que;
	que.push(temp);
	resque[cnt++] = temp;
	visted[temp.row][temp.col] = true;
	while (!que.empty())
	{
		temp = que.front();
		int x =temp.row;
		int y = temp.col;
		que.pop();		
		for(int i=0;i<4;i++)
		{
			temp.row = temp.row+step[i][0];
			temp.col = temp.col+step[i][1];
			if(arr[temp.row][temp.col] == 'T')
			{
				temp.prerow = x;
				temp.precol = y;
				resque[cnt++] = temp;
				return;
			}
			if(temp.row < 0 || temp.row >= row || temp.col < 0 || temp.col >= col)
			{
				//continue;
			}
			else
			{
				if(arr[temp.row][temp.col] == '.' && visted[temp.row][temp.col] == false)
				{
					temp.prerow = x;
					temp.precol = y;
					resque[cnt++] = temp;
					que.push(temp);
					visted[temp.row][temp.col] = true;					
				}
				
			}
			temp.row = temp.row-step[i][0];
			temp.col = temp.col-step[i][1];
		}
	}
    return;
}

int main()
{
	//fcino();
	int sx,sy,tx,ty;
	cin>>row>>col;
	for (int i=0;i<row;i++)
	{
		for (int j=0;j<col;j++)
		{
			visted[i][j] = false;
		}
	}
	char ch;
	point p;
	for (int i=0;i<row;i++)
	{
		for (int j=0;j<col;j++)
		{
			cin>>ch;
			arr[i][j] = ch;
			if(ch == 'S')
			{
			    p.row = i;
				p.col = j;
			}
			if (ch == 'T')
			{
				tx = i;
				ty = j;
			}
		}
	}
	p.prerow = -1;
	p.precol =-1;
	bfs(p);
	//cout<<cnt<<endl;
	//cout<<resque[cnt-1].row<<"   "<<resque[cnt-1].col<<endl;
	int res = 0;
	int a=resque[cnt-1].prerow,b=resque[cnt-1].precol;
	for(int i=0;i<cnt;i++)
	{
		//cout<<resque[i].prerow<<"    "<<resque[i].precol<<"               "<<resque[i].row<<"    "<<resque[i].col<<endl;
	}
	for(int i=cnt-1;i>=0;i--)
	{
		if(resque[i].prerow == -1 && resque[i].precol == -1)
		{
			break;
		}
		else
		{
			if (resque[i].row == a && resque[i].col == b)
			{
				res++;
				a=resque[i].prerow;
				b=resque[i].precol;
				//cout<<resque[i].row<<"   "<<resque[i].col<<endl;
			}
		}
	}
	if(arr[resque[cnt-1].row][resque[cnt-1].col] != 'T')
	{
		cout<<-1<<endl;
	}
	else
	{
		cout<<res+1<<endl;
	}
	//fexit();
	return 0;
}